package com.deloitte.sapnetweaver.services
{
	import flash.utils.Dictionary;
	
	import mx.rpc.remoting.mxml.RemoteObject;
	
	
	
	public class ServiceLocator
	{
		
		private static var serviceLocator : ServiceLocator;
		private var serviceMap:Dictionary = new Dictionary();
		//-----------------------------------------------------------
		public static function getInstance():ServiceLocator
		{
			if ( serviceLocator == null )
				serviceLocator = new ServiceLocator(new Enforcer());
			return serviceLocator;
		}

		//-----------------------------------------------------------
		public function ServiceLocator(enforcer:Enforcer):void
		{
			if ( serviceLocator != null )
				throw new Error( 'Only one ServiceLocator instance should be instantiated' );
		}
		
		public function getRemoteObject(destination:String,showBusyCursor:Boolean=true):RemoteObject	
		{
			if(destination && destination.length>0)
			{
				var service:RemoteObject = RemoteObject(serviceMap[destination]);
				if(service == null)
				{ 
					service = new RemoteObject(destination);					
					service.showBusyCursor = showBusyCursor;
					serviceMap[destination] = service;
				}				
				return service;
			}
			else
			{
				throw new Error( 'Can not create  service,Please check destination' );
			}
			return null;
		}	
	}
}
class Enforcer{}